package com.example.vo;

import com.example.constant.ErrorCode;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@Data
public class Result implements Serializable {

    private static final long serialVersionUID = 6758722598052481767L;

    /**
     * 错误码
     */
    private int status;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * 请求结果，返回数据
     */
    private Object data;

    public static Result error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统错误！");
    }

    public static Result error(int status) {
        Result result = new Result();
        result.setStatus(status);
        return result;
    }

    public static Result error(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        return result;
    }

    public static Result error(int status, String msg) {
        Result result = new Result();
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public static Result error(ErrorCode errorCode) {
        return putErrorCode(errorCode);
    }

    public static Result success() {
        return success("操作成功");
    }

    public static Result success(String msg) {
        Result result = putErrorCode(ErrorCode.SUCCESS);
        result.setMsg(msg);
        return result;
    }

    public static Result success(Object data) {
        Result result = putErrorCode(ErrorCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result success(String msg, Object data) {
        Result result = putErrorCode(ErrorCode.SUCCESS);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    private static Result putErrorCode(ErrorCode errorCode) {
        Result result = new Result();
        result.setStatus(errorCode.getCode());
        result.setMsg(errorCode.getMsg());
        return result;
    }
}
